package com.cc.service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import com.cc.dao.UserStudentMapper;
import com.cc.entity.UserStudent;
import com.cc.utils.Idgen;

@Service
public class UserStudentService {
	@Resource  
    private UserStudentMapper userStudentDao;
	public int sign(UserStudent userStudent){
		//验证用户名是否重复
		UserStudent record=userStudentDao.selectUserStudentByUsername(userStudent);
		if(record!=null) return 1;
		//生成uuid
		userStudent.setId(Idgen.getUUID());
		//插入数据
		userStudentDao.insert(userStudent);
		//返回状态，0成功，1失败
		return 0;
	}
	 public int login(UserStudent userStudent){
		 UserStudent record=userStudentDao.selectUserStudentByUsername(userStudent);
		 if(record==null) return 1;//用户名不存在
		 String inputpaw=userStudent.getPassword();
		 String targetpaw=record.getPassword();
		 if(inputpaw.equals(targetpaw)){
			 userStudent=record;
			 return 0;//验证成功
		 }else
			 return 2;//密码错误
	 }
	 
     public int resetUserStudent(String key,String val,UserStudent currentUser,String lab){
    	 switch (key.trim()) {
			case "姓名":
				currentUser.setName(val.trim());
				break;
			case "性别":
				currentUser.setSex(val.trim());
				break;
			case "年龄":
				currentUser.setAge(Integer.parseInt(val.trim()));
				break;
			case "电话":
				currentUser.setPhonenum(val.trim());
				break;
			case "学位":
				currentUser.setDegree(val.trim());
				break;
			case "入学年份":
				currentUser.setEnrollmentYear(val.trim());
				break;
			case "毕业学校":
				String oldscid=currentUser.getSchoolid();
				currentUser.setSchoolid(val.trim());
				currentUser.setSchoolname(lab.trim());
				if(!val.trim().equals(oldscid)){
					currentUser.setInstituteid("");
					currentUser.setInstitutename("");
					currentUser.setMajorid("");
					currentUser.setMajorname("");
				}
				
				break;
			case "学院":
				String oldinid=currentUser.getInstituteid();
				currentUser.setInstituteid(val.trim());
				currentUser.setInstitutename(lab.trim());
				if(!val.trim().equals(oldinid)){
					currentUser.setMajorid("");
					currentUser.setMajorname("");
				}
				
				break;
			case "专业":
				currentUser.setMajorid(val.trim());
				currentUser.setMajorname(lab.trim());
				break;
			case "就职公司":
				currentUser.setEnterprisename(val.trim());
				break;
			case "公司类型":
				currentUser.setEnterprisetype(val.trim());
				break;
			case "工作岗位":
				currentUser.setPosition(val.trim());
				break;
			case "工龄":
				currentUser.setEntrytime(Integer.parseInt(val.trim()));
				break;
			case "年薪":
				currentUser.setSalary(Integer.parseInt(val.trim()));
				break;
			
			default:
				break;
			}
    	 userStudentDao.updateByPrimaryKey(currentUser);
    	 return 0;
     }
}
